/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.contentv2.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jzwl.common.constant.AjaxStatusConstant;
import com.jzwl.common.page.PageObject;
import com.jzwl.common.upload.service.UploadImgService;
import com.jzwl.site.manager.contentv2.service.SiteContentV2Service;
import com.jzwl.system.base.controller.BaseController;

/**
 * SiteContentV2
 * SiteContentV2
 * siteContentV2
 * <p>Title:SiteContentV2Controller </p>
 * 	<p>Description:SiteContentV2 </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller
@RequestMapping("/siteContentV2")
public class SiteContentV2Controller extends BaseController {

	@Autowired
	private SiteContentV2Service siteContentV2Service;
	@Autowired
	private UploadImgService uploadImgService;

	/**
	 * 跳转到添加SiteContentV2的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddSiteContentV2")
	public ModelAndView toAddsiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		List<Map<String, Object>> coulmnlist = siteContentV2Service.querySelectValue(paramsMap);//栏目列表
		createParameterMap(request);
		mov.addObject("coulmnlist", coulmnlist);
		mov.setViewName("/manager/contentv2/add");
		return mov;
	}

	/**
	 * 添加SiteContentV2
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addSiteContentV2")
	public ModelAndView addsiteContentV2(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			boolean flag = siteContentV2Service.addSiteContentV2(paramsMap);
			if (!flag) {
				mov.addObject("msg", "添加SiteContentV2失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteContentV2/siteContentV2List.html");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "添加SiteContentV2失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * siteContentV2list
	 */
	@RequestMapping(value = "/siteContentV2List")
	public ModelAndView siteContentV2List(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		PageObject po = new PageObject();

		try {

			po = siteContentV2Service.querySiteContentV2List(paramsMap);

			mov.addObject("list", po.getDatasource());
			mov.addObject("totalProperty", po.getTotalCount());
			mov.addObject("po", po);
			mov.addObject("paramsMap", paramsMap);
			mov.setViewName("/manager/contentv2/list");
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "siteContentV2查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 跳转到修改siteContentV2页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toUpdateSiteContentV2")
	public ModelAndView toUpdateSiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			List<Map<String, Object>> coulmnlist = siteContentV2Service.querySelectValue(paramsMap);//栏目列表
			Map<String, Object> siteContentV2 = siteContentV2Service.getByContenId(paramsMap);
			mov.addObject("obj", siteContentV2);
			mov.addObject("coulmnlist", coulmnlist);
			mov.setViewName("/manager/contentv2/update");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteContentV2查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 跳转到详情siteContentV2页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailSiteContentV2")
	public ModelAndView detailSiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			Map<String, Object> siteContentV2 = siteContentV2Service.getByContenId(paramsMap);
			mov.addObject("obj", siteContentV2);
			mov.setViewName("/manager/contentv2/detail");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteContentV2查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 修改siteContentV2
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateSiteContentV2")
	public ModelAndView updateSiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteContentV2Service.updateSiteContentV2(paramsMap);

			if (!flag) {
				mov.addObject("msg", "修改失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteContentV2/siteContentV2List.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "修改失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 逻辑删除siteContentV2
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delSiteContentV2")
	public ModelAndView delSiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteContentV2Service.deleteSiteContentV2(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteContentV2/siteContentV2List.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 物理删除siteContentV2
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteSiteContentV2")
	public ModelAndView deleteSiteContentV2(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteContentV2Service.deleteSiteContentV2(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteContentV2/siteContentV2List.html");
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 
	 * 项目名称：auto-code-v5
	 * 描述：异步上传图片，单张
	 * 创建人： ln
	 * 创建时间： 2016年9月6日
	 * 标记：manager
	 * @param img
	 * @param request
	 * @param response
	 * @return
	 * @version
	 * para1:type表示上传到某一个资源上去1：表示静态资源，2：表示上传到oss服务器上去
	 * para2:img 要上传的图片
	 * para3:flag 当type 为2时 flag表示是那个项目的图片eg：bbs，tg,zb..当为1时可传null,
	 */
	@RequestMapping(value = "uploadImg", method = RequestMethod.POST)
	public @ResponseBody Map uploadImg(@RequestParam(required = false) MultipartFile pic, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			int type = 1;
			String url = uploadImgService.uploadImg(type, pic);//返回全路径
			map.put("path", url);
			map.put("url", url);
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//auto-code-end

}
